<?php

use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Context\ApplicationContext;
use Psr\Http\Message\ServerRequestInterface;


/**
 * 容器实例
 */
if (!function_exists('container')) {
    function container()
    {
        return ApplicationContext::getContainer();
    }
}

/**
 * request()
 */
if (!function_exists('request')) {
    function request()
    {
        return container()->get(ServerRequestInterface::class);
    }
}

/**
 * response()
 */
if (!function_exists('response')) {
    function response()
    {
        return container()->get(ResponseInterface::class);
    }
}

/**
 * apiJson()
 */
if (!function_exists('apiJson')) {
    function apiJson(int $code = 1, string $msg = '', $data = [])
    {
        return response()->json(
            [
                'code' => $code,
                'msg' => $msg,
                'data' => $data,
            ]
        );
    }
}


/**
 * //获取请求者ip
 * getIp()
 */
if (!function_exists('getIp')) {
    function getIp()
    {
        $headers = request()->getHeaders();
        if (isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
            return $headers['x-forwarded-for'][0];
        } elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
            return $headers['x-real-ip'][0];
        }
        $serverParams = request()->getServerParams();
        return $serverParams['remote_addr'] ?? '';
    }
}


/**
 * 解析json串
 * @param type $json_str
 * @return type
 */
function analyJson($json_str)
{
    $json_str = str_replace('＼＼', '', $json_str);
    $out_arr = array();
    preg_match('/{.*}/', $json_str, $out_arr);
    if (!empty($out_arr)) {
        $result = json_decode($out_arr[0], TRUE);
    } else {
        return FALSE;
    }
    return $result;
}

/**
 * 把受保护的对象转通用
 */
if (!function_exists('accessProtected')) {
    /**
     * @param $obj 受保护的对象
     * @param $prop 要获取的对象
     * @return mixed
     * @throws ReflectionException
     */
    function accessProtected($obj, $prop)
    {
        $reflection = new \ReflectionClass($obj);
        $property = $reflection->getProperty($prop);
        $property->setAccessible(true);
        return $property->getValue($obj);
    }
}


/**
 * 把受保护的对象转通用
 */
if (!function_exists('arrayCache')) {
    /**
     * @param $arr array 要缓存的数组
     * @return mixed
     * @throws ReflectionException
     */
    function arrayCache($arr, $cache_name)
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(\Hyperf\Redis\Redis::class);
        $redis->set($cache_name,$arr,600);
    }
}



/**
 * 通过开始、结束时间获取范围数组
 * @param $startDate
 * @param $endDate
 * @return array
 */
function loopTimeRange($startDate, $endDate, $format = 'Y-m-d')
{
    $timeArray = [];
    $currentDate = new DateTime($startDate);
    $endDate = new DateTime($endDate);
    $interval = new DateInterval('P1D'); // 设置间隔为一天

    while ($currentDate <= $endDate) {
        $timeArray[] = $currentDate->format($format); // 将日期添加到数组中，并可以按需要格式化
        $currentDate->add($interval); // 增加一天间隔
    }

    return $timeArray;
}






